﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ChemicalEquilibrium
{
    /// <summary>
    /// 电压门控
    /// </summary>
    public class VoltageGated
    {
        public Reactants Channel;
        public int VoltageOpen, VoltageClose, VoltageActive, VoltageInactive, Timeout;
        public GetVoltageCallback GetVoltage;

        int ticks = 0;
        bool IsOpened = false;
        bool IsActive = false;

        public VoltageGated(Reactants channel, int voltageOpen, int voltageClose, int voltageActive, int voltageInactive, int timeout, GetVoltageCallback getVoltage)
        {
            this.Channel = channel;
            this.VoltageOpen = voltageOpen;
            this.VoltageClose = voltageClose;
            this.VoltageActive = voltageActive;
            this.VoltageInactive = voltageInactive;
            this.Timeout = timeout;

            this.GetVoltage = getVoltage;

            
        }

        /// <summary>
        /// 门控
        /// </summary>
        public void Transport()
        {
            int voltage = this.GetVoltage();

            if (voltage < this.VoltageActive) this.IsActive = true;
            else if (voltage > this.VoltageInactive) this.IsActive = false;

            if (!this.IsOpened && this.IsActive)
            {
                if (voltage > this.VoltageOpen)
                {
                    this.ticks = this.Timeout;
                    this.IsOpened = true;
                }
            }
            else if (this.IsOpened && this.IsActive)
            {
                if (this.ticks > 0)
                    this.ticks--;
                else
                {
                    this.IsOpened = false;
                    this.IsActive = false;
                }
            }

            if (this.IsOpened && this.IsActive)
            {
                this.Channel.Activate(this.Channel.Inactive.Count);
            }
            else
            {
                this.Channel.Inactivate(this.Channel.Active.Count);
            }
        }
    }
}
